<?
############################################################################
#    Copyright (C) 2010 by math2gold                                       #
#    Twitter:@math2gold                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

require("xml2array.php");
require("array2xml.php");
function make_bridge_key_pair($key)
{
 $b=array();
 $p=strlen($key);
 $q=(int)($p/2);
 $key1=substr($key,0,$q);
 $key2=substr($key,$q,$p);
 $b[]=$key1;
 $b[]=$key2;
 return $b;
}

function convert_params_to_xml($params)
{
    $xml = new array2xml('a');
    $xml->createNode( $params);
    $p=(string)$xml;	
    return  substr(strstr($p,"?>"),3);
}

function convert_xml_to_params($xmlstr)
{
	$xmlObj    = new XmlToArray($xmlstr);
	$arrayData = ($xmlObj->createArray());	
	return $arrayData["a"]; 
}

function make_params_sec($params,$keypairs)
{  
     $x=$params;
     ksort($x);
     $v="";
     foreach ( $x as $g => $t){
       $v = $v . '@' . $params[$g] .'@';
     }
     $v=$keypairs[0] . $v . $keypairs[1];
     $params["seckey"]=md5($v);
     $params["sectype"]="md5";
     return $params;
}

function check_params_sec($params,$keypairs)
{
	$seckey=$params["seckey"];
	$sectype=$params["sectype"];
	if (!($sectype=="md5")) return null;
	unset($params["sectype"]);
	unset($params["seckey"]);
	$g=make_params_sec($params,$keypairs);
	if ($g["sectype"]==$sectype && $g["seckey"]==$seckey)
	{
		return $params;
	}
	return null;
}
function bridge_send_cmd($url,$params,$keypairs)
{
   $data=convert_params_to_xml(make_params_sec($params,$keypairs));
   $ch=curl_init($url);
   $headers  =  array( "User-Agent: io43.Math2Gold Bridge" );
   curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
   curl_setopt($ch,CURLOPT_HEADER,0);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
   #curl_setopt($ch,CURLOPT_USERPWD,$auth);
   curl_setopt($ch,CURLOPT_PROXY,'192.168.0.11:7077');
   curl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
   $result=curl_exec($ch);
   curl_close($ch);
   return $result;  	
}

function bridge_send_text_msg($hubid,$url,$key,$userid,$userfid,$user_nick,$msg)
{
$x=array();
$x["id"]=$hubid;
$x["userid"]=$userid;
$x["userfid"]=$userfid;
$x["user_nick"]=$user_nick;
$x["msg"]=$msg;
$x["ts"]="".time();
$x["cmd"]="msg";
return bridge_send_cmd($url,$x,make_bridge_key_pair($key));
}
?>
